//
//  TreeViewController.swift
//  HakimSMBDemo
//
//  Created by 范华泽 on 2017/3/17.
//  Copyright © 2017年 Hakim. All rights reserved.
//

import UIKit

class TreeViewController: UIViewController {
    
    var dataSource = NSMutableArray()
    
    public var path:String? {
        didSet{
            path = "smb://" + path!
            
            self.title = path
            
            //根据path刷新获取当前所有的数据
            self.requestSMBData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        //添加一个tableview用来显示当前局域网内所有的共享客户端
        self.view.addSubview(self.tableView)
    }
    
    //根据path刷新获取当前所有的数据
    func requestSMBData() {
        let provider:KxSMBProvider = KxSMBProvider.sharedSmbProvider() as! KxSMBProvider
        provider.fetch(atPath: self.path as String!) { (result:Any) in
            if ((result as? NSArray) != nil) {
                self.dataSource = NSMutableArray.init(array: (result as! NSArray))
            }else if ((result as? KxSMBItem) != nil) {
                self.dataSource = NSMutableArray.init(array: [result])
            }
            
            self.tableView.reloadData()
        }
    }
    
    fileprivate lazy var tableView:UITableView = {
        let tv = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight), style: UITableViewStyle.plain)
        tv.delegate = self
        tv.dataSource = self
        tv.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
        return tv
    }()
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

//MARK:UITableViewDelegate
extension TreeViewController:UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        tableView.deselectRow(at: indexPath, animated: true)
        
        let item = self.dataSource[indexPath.row] as! KxSMBItem
        if ((item as! KxSMBItemTree) != nil) {
            let treeVC = TreeViewController()
            self.path = item.path
            self.navigationController?.pushViewController(treeVC, animated: true)
        } else if ((item as! KxSMBItemFile) != nil){
            print("文件")
        }
    }
}

//MARK:UITableViewDataSource
extension TreeViewController:UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(
            withIdentifier: "cellId")!
        
        let itme:KxSMBItem = self.dataSource[indexPath.row] as! KxSMBItem
        cell.textLabel?.text = (itme.path as NSString).lastPathComponent
        
        if ((itme as? KxSMBItemTree) != nil) {
            cell.accessoryType = .disclosureIndicator
        }else{
            cell.accessoryType = .none
            cell.detailTextLabel?.text = "\(itme.stat.size)"
        }
        
        return cell
    }
}


